package com.tools.utils;

import com.tools.entity.DatabaseContent;
import com.tools.entity.EntityContent;
import com.tools.entity.FieldAttribute;
import com.tools.entity.TableAttributeEntity;
import com.tools.model.DataBaseModel;
import com.tools.model.EntityModel;

import java.util.ArrayList;

/**
 * 转换器
 *
 * @author < a href=" ">ZhangYang</ a>
 * @version v1.0.01
 * @date 2018-08-01 14:23:12
 */

public class ConverterUtil {
  /**
   * 将实体类属性信息转换为模板类需要用的上下文属性
   *
   * @param content 装换目标类
   */
  public static void databaseConfigToContent(DataBaseModel dc, DatabaseContent content) {
    content.setDisplayName(dc.getConnName());
    content.setHost(dc.getConnURL());
    content.setPort(dc.getListenPort());
    content.setUserName(dc.getUserName());
    content.setUserPwd(dc.getUserPwd());
    content.setDbName(dc.getDbName());
    content.setDbType(dc.getDbType());
    content.setEncoding(dc.getEncoding());
  }

  /**
   * 将实体类属性信息转换为模板类需要用的上下文属性
   *
   * @param ec 实体类信息
   * @param content 装换目标类
   */
  public static void entityConfigToContent(EntityModel ec, EntityContent content) {
    if (!StringUtil.isNullOrEmpty(ec.getTableAlias())) {
      content.setTableAlias(ec.getTableAlias());
    }
    if (!StringUtil.isNullOrEmpty(ec.getPrimaryKey())) {
      content.setPrimaryKey(ec.getPrimaryKey());
    }
    if (ec.getTblPropertyValues() != null) {
      ArrayList<FieldAttribute> list = new ArrayList<>();
      ArrayList<FieldAttribute> cantNullAttrs = null;
      ArrayList<FieldAttribute> otherAttrs = null;

      for (TableAttributeEntity c : ec.getTblPropertyValues()) {
        if (!c.getTdCreate()) {
          continue;
        }
        FieldAttribute attr = new FieldAttribute(c);
        attr.setField(c.getTdField());
        // 列名首字母大写
        String upField = StringUtil.fristToUpCase(c.getTdField());
        attr.setFieldPascal(upField);
        // java数据类型
        String javaType = attr.getJavaType();
        if ("boolean".equalsIgnoreCase(javaType)) {
          attr.setFget("is" + upField);
        } else {
          attr.setFget("get" + upField);
        }
        attr.setFset("set" + upField);
        attr.setFgetType("get" + javaType);
        attr.setFsetType("set" + javaType);
        // 添加属性到所有属性
        list.add(attr);
        // 设置主键的类型与主键的属性列
        if (ec.getPrimaryKey() != null && ec.getPrimaryKey().equals(c.getTdColumnName())) {
          content.setPrimaryKeyJdbcType(c.getTdJdbcType());
          content.setPrimaryKeyJavaType(c.getTdJavaType().getValue());
          content.setPrimaryKeyAttr(attr);
          continue;
        }
        // 添加不能为空的属性属性列与其他属性
        if (!attr.isNullable() && StringUtil.isNullOrEmpty(attr.getColumnDef())) {
          if (cantNullAttrs == null) {
            cantNullAttrs = new ArrayList<>();
          }
          cantNullAttrs.add(attr);
        } else {
          if (otherAttrs == null) {
            otherAttrs = new ArrayList<>();
          }
          otherAttrs.add(attr);
        }
      }
      content.setAttrs(list);
      content.setCantNullAttrs(cantNullAttrs);
      content.setOtherAttrs(otherAttrs);
    }
  }

}
